package test

/*
Copyright 2021-2025 The k8gb Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"k8gbterratest/utils"
	"path/filepath"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestAnnotations(t *testing.T) {
	t.Parallel()
	var tests = []struct {
		name            string
		host            string
		path            string
		patch           map[string]string
		expectedGslb    map[string]string
		expectedIngress map[string]string
	}{
		{
			name:            "Create From Ingress - patch non k8gb annotation",
			host:            "test-ingress-annotation-failover.cloud.example.com",
			path:            "../examples/ingress-annotation.yaml",
			patch:           map[string]string{"example.io/protocol": "tcp"},
			expectedIngress: map[string]string{"k8gb.io/primary-geotag": "eu", "k8gb.io/strategy": "failover", "example.io/protocol": "tcp"},
			expectedGslb:    map[string]string{},
		},
		{
			name:            "Create From Ingress - patch k8gb annotation",
			host:            "test-ingress-annotation-failover.cloud.example.com",
			path:            "../examples/ingress-annotation.yaml",
			patch:           map[string]string{"k8gb.io/dns-ttl-seconds": "100"},
			expectedIngress: map[string]string{"k8gb.io/primary-geotag": "eu", "k8gb.io/strategy": "failover", "k8gb.io/dns-ttl-seconds": "100"},
			expectedGslb:    map[string]string{},
		},
		{
			name:            "Create From Ingress - patch with existing k8gb annotation",
			host:            "test-ingress-annotation-failover.cloud.example.com",
			path:            "../examples/ingress-annotation.yaml",
			patch:           map[string]string{"k8gb.io/primary-geotag": "us"},
			expectedIngress: map[string]string{"k8gb.io/primary-geotag": "us", "k8gb.io/strategy": "failover"},
			expectedGslb:    map[string]string{},
		},
		{
			name:            "Create From GSLB - patch non k8gb annotation",
			host:            "test-gslb-annotation.cloud.example.com",
			path:            "../examples/gslb-annotation.yaml",
			patch:           map[string]string{"example.io/protocol": "tcp"},
			expectedIngress: map[string]string{"example.io/protocol": "tcp"},
			expectedGslb:    map[string]string{"example.io/origin": "gslb"},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			testAnnotations(t, test)
		})

	}
}

func testAnnotations(t *testing.T, test struct {
	name            string
	host            string
	path            string
	patch           map[string]string
	expectedGslb    map[string]string
	expectedIngress map[string]string
}) {
	// Path to the Kubernetes resource config we will test
	ingressPath, err := filepath.Abs(test.path)
	require.NoError(t, err)

	workflowEU := utils.NewWorkflow(t, "k3d-test-gslb1", 5053).
		WithGslb(ingressPath, test.host).
		WithTestApp(test.name)
	if ingressPath != "" {
		workflowEU = workflowEU.WithIngress(ingressPath)
	}
	instanceEU, err := workflowEU.Start()
	require.NoError(t, err)
	defer instanceEU.Kill()

	err = instanceEU.WaitForAppIsRunning()
	require.NoError(t, err)

	err = instanceEU.WaitForExternalDNSEndpointExists("cloud.example.com")
	require.NoError(t, err)

	err = instanceEU.Resources().Ingress().PatchAnnotations(test.patch)
	require.NoError(t, err)
	time.Sleep(10 * time.Second)
	newAnnotationsIngress := instanceEU.Resources().Ingress().GetAnnotations()
	newAnnotationsGslb := instanceEU.Resources().Gslb().GetAnnotations()

	delete(newAnnotationsIngress, "kubectl.kubernetes.io/last-applied-configuration")
	delete(newAnnotationsGslb, "kubectl.kubernetes.io/last-applied-configuration")
	assert.True(t, utils.EqualAnnotations(test.expectedIngress, newAnnotationsIngress))
	assert.True(t, utils.EqualAnnotations(test.expectedGslb, newAnnotationsGslb))
}
